local skel = fk.CreateSkill {
  name = "efengqi__danqi",
}

Fk:loadTranslationTable{
  ["efengqi__danqi"] = "啖弃",
  [":efengqi__danqi"] = "当你的判定牌生效后，若结果为：红色，你受到1点伤害；黑色，你可以弃置场上一张牌或获得中央区一张牌。",

  ["#efengqi__danqi-invoke"] = "啖弃：你可以弃置场上一张牌或获得中央区一张牌",
  ["#efengqi__danqi-throw"] = "啖弃：你可以选择一名角色，弃置其场上一张牌。不选则改为你获得中央区一张牌",
}

--- 获得中央区牌，防止没加载DIY Utility
---@param room Room
---@return integer[]
local getCenterCards = function (room)
  if Fk.skills["#CenterArea"] then
    return room:getBanner("@$CenterArea")
  else
    local ids = {}
    room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
      for _, move in ipairs(e.data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            table.insertIfNeed(ids, info.cardId)
          end
        end
      end
      return false
    end, Player.HistoryTurn)
    ids = table.filter(ids, function (id)
      return room:getCardArea(id) == Card.DiscardPile
    end)
    return ids
  end
end

skel:addEffect(fk.FinishJudge, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) and data.card.color ~= Card.NoColor then
      return data.card.color == Card.Red or (#getCenterCards(player.room) > 0
        or table.find(player.room.alive_players, function(p) return #p:getCardIds("ej") > 0
      end))
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if data.card.color == Card.Red then
      return true
    else
      if room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#efengqi__danqi-invoke"}) then
        local targets = table.filter(room.alive_players, function (p) return #p:getCardIds("ej") > 0 end)
        local cost_data = { tos = {} }
        if #targets > 0 then
          local tos = room:askToChoosePlayers(player, { targets = targets , min_num = 1, max_num = 1,
           prompt = "#efengqi__danqi-throw", skill_name = skel.name })
          cost_data.tos = tos
        end
        event:setCostData(self, cost_data)
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if data.card.color == Card.Red then
      room:damage { from = nil, to = player, damage = 1, skillName = skel.name }
    else
      local tos = event:getCostData(self).tos
      if #tos > 0 then
        local to = tos[1]
        if #to:getCardIds("ej") > 0 then
          local cid = room:askToChooseCard(player, { target = to, flag = "ej", skill_name = skel.name})
          room:throwCard(cid, skel.name, to, player)
        end
      else
        local cards = getCenterCards(room)
        if #cards > 0 then
          local card = room:askToChooseCard(player, { target = player, flag = { card_data = { { skel.name, cards } } }, skill_name = skel.name, prompt = "$ChooseCard"})
          room:obtainCard(player, card, true, fk.ReasonJustMove, player, skel.name)
        end
      end
    end
  end,
})

skel:addAcquireEffect(function (self, player, is_start)
  player.room:addSkill("#CenterArea")
end)

return skel
